package com.mall.common.util;

import java.util.Date;
import java.util.regex.Pattern;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 验证工具类 
 *
 */
public class VerifyUtil {
	/** 存入数据库  验证码分隔符 **/
	public static final String VERIFY_CODE_KEY_SEPARATOR = "_";
	/** 存入数据库  邮箱验证码分隔符 **/
	public static final String VERIFY_EMAIL_CODE_KEY_SEPARATOR = ",";
	/** 验证码过期时间间隔 **/
	public static final int VERIFY_CODE_KEY_PERIOD = 30;
	
	/**
	 * 验证身份证
	 * @param idCard 身份证号
	 * @param isNullable 是否可空
	 * @return
	 */
	public static boolean verifyIDCard(String idCard, boolean isNullable){
		if(isNullable && StringUtils.isBlank(idCard)){
			return true;
		}
		if(!Pattern.matches("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$", idCard)
				&& !Pattern.matches("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$", idCard)){
			return false;
		}
		return true;
	}
	
	/**
	 * 验证手机号码
	 * @param mobile 手机号码
	 * @param isNullable 是否可空
	 * @return
	 */
	public static boolean verifyMobile(String mobile, boolean isNullable){
		if(isNullable && StringUtils.isBlank(mobile)){
			return true;
		}
		return Pattern.matches("^(13\\d|145|147|15[0-3]|15[5-9]|177|18[0-3]|18[5-9])\\d{8}$", mobile);
	}
	
	/**
	 * 验证邮箱
	 * @param email 邮箱
	 * @param isNullable 是否可空
	 * @return
	 */
	public static boolean verifyEmail(String email, boolean isNullable){
		if(StringUtils.isBlank(email)){
			return isNullable;
		}
		return Pattern.matches("^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)$", email);
	}
	
	/**
	 * 验证用户名
	 * @param username
	 * @param isNullable
	 * @return
	 */
	public static boolean verifyUsername(String username, boolean isNullable){
		if(isNullable && StringUtils.isBlank(username)){
			return true;
		}
		return !Pattern.matches("^[a-zA-Z]+$", username)
				&& !Pattern.matches("^\\d+$", username)
				&& !Pattern.matches("^_+$", username)
				&& Pattern.matches("^[a-zA-Z0-9_-]{5,25}$", username);
	}
	
	/**
	 * 创建邮箱验证码
	 * @param valCode
	 * @return 时间_验证码
	 */
	public static String createEmailVerifyCodeKey(String email, String valCode){
		StringBuffer verifyCodeKey = new StringBuffer();
		verifyCodeKey.append(valCode);
		verifyCodeKey.append(VERIFY_EMAIL_CODE_KEY_SEPARATOR);
		verifyCodeKey.append(System.currentTimeMillis());
		verifyCodeKey.append(VERIFY_EMAIL_CODE_KEY_SEPARATOR);
		verifyCodeKey.append(email);
		return verifyCodeKey.toString();
	}
	
	/**
	 * 验证邮箱验证码
	 * @param oldKey  验证码加密串
	 * @param newKey  输入验证码
	 * @return
	 */
	public static String checkEmailVerify(String oldKey, String newKey, Integer expired){
		String[] oldKeys = oldKey.split(VERIFY_EMAIL_CODE_KEY_SEPARATOR);
		if(!newKey.equals(oldKeys[0])){
			return "邮箱验证码已失效！";
		}
		if(expired == null){
			expired = VERIFY_CODE_KEY_PERIOD;
		}
		Date recoverKeyBuildDate = new Date(Long.valueOf(oldKeys[1]));
		Date recoverKeyExpiredDate = DateUtils.addMinutes(recoverKeyBuildDate, expired);
		Date now = new Date();
		if (now.after(recoverKeyExpiredDate)) {
			return "对不起，此验证码已过期！";
		}
		return null;
	}
	
	/**
	 * 根据加密串获取邮箱
	 * @param verifyCodeKey 数据库加密串
	 * @return
	 */
	public static String getEmailFromVerifyCodeKey(String verifyCodeKey){
		if(StringUtils.isBlank(verifyCodeKey)){
			return null;
		}
		String[] splitStrs = verifyCodeKey.split(VERIFY_EMAIL_CODE_KEY_SEPARATOR);
		if(splitStrs.length < 3){
			return null;
		}
		String email = splitStrs[2];
		if(verifyEmail(email, false)){
			return email;
		}
		return null;
	}
	
}
